package com.shiwaixiangcun.monitor.controller;

import com.shiwaixiangcun.core.plugin.dto.MonkeyDto;
import com.shiwaixiangcun.core.plugin.report.pdf.PdfData;
import com.shiwaixiangcun.core.plugin.web.validator.ConstraintType;
import com.shiwaixiangcun.core.plugin.web.validator.ParameterConstraint;
import com.shiwaixiangcun.core.plugin.web.validator.ParameterConstraints;
import com.shiwaixiangcun.core.web.response.ResponseEntity;
import com.shiwaixiangcun.monitor.dto.WorkOrderListDto;
import com.shiwaixiangcun.monitor.dto.WorkOrderQueryDto;
import com.shiwaixiangcun.monitor.service.WorkOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.validation.constraints.NotNull;

/**
 * Created by Administrator on 2017-11-23.
 */
@Controller
@RequestMapping(value = "/work/order")
public class WorkOrderController {

    @Autowired
    private WorkOrderService workOrderService;


    @ParameterConstraints({
            @ParameterConstraint(name = "priorityLevel", constraintType = {ConstraintType.NotNull}, notNull = @NotNull(message = "优先级不能为空")),
            @ParameterConstraint(name = "title", constraintType = {ConstraintType.NotNull}, notNull = @NotNull(message = "标题不能为空")),
            @ParameterConstraint(name = "categoryId", constraintType = {ConstraintType.NotNull}, notNull = @NotNull(message = "工单类别不能为空")),
            @ParameterConstraint(name = "staffId", constraintType = {ConstraintType.NotNull}, notNull = @NotNull(message = "受理人为空"))
    })
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String addWorkOrder(MonkeyDto monkeyDto) {
        workOrderService.addWorkOrder(monkeyDto);
        return "SUCCESS:新建成功";
    }


    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public WorkOrderListDto getWorkOrderList(Pageable pageable, WorkOrderQueryDto workOrderQueryDto) {
        return workOrderService.getWorkOrderList(pageable, workOrderQueryDto);
    }

    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public ResponseEntity detail(@RequestParam Long id) {
        return new ResponseEntity().success(workOrderService.getDetail(id));
    }

    @RequestMapping(value = "/reply", method = RequestMethod.POST)
    public String reply(@RequestParam Long id, String content) {
        workOrderService.handle(id, content);
        return "SUCCESS:回复成功";
    }

    @RequestMapping(value = "/finish", method = RequestMethod.PUT)
    public String finish(@RequestParam Long id) {
        workOrderService.handle(id);
        return "SUCCESS:已完结";
    }

    @RequestMapping(value = "/transfer", method = RequestMethod.PUT)
    public String transfer(@RequestParam Long id, @RequestParam Long userId) {
        workOrderService.handle(id, userId);
        return "SUCCESS:已转交";
    }

    @RequestMapping(value = "/print", method = RequestMethod.GET)
    public PdfData print(@RequestParam Long id) {
        return workOrderService.print(id);
    }

}
